﻿@MenuItem("Project Tools/Make Prefab")
static function MakePrefab(){
	GeneratePrefab();
}

static function GeneratePrefab(){
	var selectedObjects : GameObject[] = Selection.gameObjects;
	for (var selObject: GameObject in selectedObjects){
		var name:String = selObject.name;
		var path:String = "Assets/Prefabs/" + name + ".prefab";
		if (AssetDatabase.LoadAssetAtPath(path,GameObject)){
			if(EditorUtility.DisplayDialog("Caution","Prefab already exists. Do you want to overwrite?","Yes","No")){
				createNew(selObject,path);
			}		
		} 
		else{
			createNew(selObject,path);
		}
	}
}

static function createNew(selObject :GameObject ,path : String){
	var prefab:Object = PrefabUtility.CreateEmptyPrefab(path);
	PrefabUtility.ReplacePrefab(selObject,prefab);
	
	DestroyImmediate(selObject);
	var prefabClone:GameObject = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
	
	
	AssetDatabase.Refresh();
}